package com.pubinfo.passbook.common.service.uccp.impl;

import cn.hutool.core.util.StrUtil;
import com.pubinfo.passbook.common.entity.TPassbook;
import com.pubinfo.passbook.common.entity.TSourceArtificial;
import com.pubinfo.passbook.common.entity.TWiteoffInstance;
import com.pubinfo.passbook.common.mapper.TPassbookPersionInfoMapper;
import com.pubinfo.passbook.common.mapper.TSourceArtificialMapper;
import com.pubinfo.passbook.common.mapper.TSourcePassbookMapper;
import com.pubinfo.passbook.common.model.bo.uccp.ShortMessageParam;
import com.pubinfo.passbook.common.model.ruler.PayInformation;
import com.pubinfo.passbook.common.service.dubbo.DubboService;
import com.pubinfo.passbook.common.service.master.TWiteoffInstanceService;
import com.pubinfo.passbook.common.service.ruler.InformationService;
import com.pubinfo.passbook.common.service.uccp.UccpService;
import com.pubinfo.passbook.common.utils.UccpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author litiewang
 * @version 1.0
 * @date 2021/12/15 17:03
 */
@Service
public class UccpServiceImpl implements UccpService {
  @Autowired
  TWiteoffInstanceService instanceService;
  @Autowired
  DubboService vatService;
  @Autowired
  TSourcePassbookMapper passbookMapper;
  @Autowired
  TPassbookPersionInfoMapper infoMapper;
  @Autowired
  TSourceArtificialMapper artificialMapper;
  
  @Autowired
  InformationService informationService;
  
  @Override
  public Boolean sendShortMessage(List<String> witeoffIds, String idNo) {
    List<TWiteoffInstance> instances = instanceService.listByIds(witeoffIds);
    if (CollectionUtils.isEmpty(instances)) {
      return false;
    }
    Integer countTimeout = 0;
    List<TPassbook> passbookList = passbookMapper.findPassbookByWiteoffids(witeoffIds);
    if (!CollectionUtils.isEmpty(passbookList)) {
      //省公司才过滤
      List<String> nsrsbhs = new ArrayList<>();
      nsrsbhs.add("91330000MA27U07PX0");
      nsrsbhs.add("91330000744141180B");
      //复制一份
      List<TPassbook> passbooks = new ArrayList<>(passbookList);
      List<PayInformation> payInformations = this.informationService.listInformationFromPassbook(passbooks.stream().filter(tPassbook -> nsrsbhs.contains(tPassbook.getNsrsbh())).map(TPassbook::getEInvoiceId).collect(Collectors.toList()));
      countTimeout = UccpUtil.isTimeout(payInformations);
    }
    
    List<TSourceArtificial> artificialList = artificialMapper.listArtificialByWiteoffId(witeoffIds);
    //如果单据不为空，
    String dateStr = UccpUtil.getSubmitDate(passbookList);
    BigDecimal sum = BigDecimal.ZERO;
    String code = "";
    String msg;
    String comma = "，";
    for (TWiteoffInstance instance : instances) {
      code += instance.getWiteoffInstanceCode().concat(comma);
      sum = sum.add(instance.getSum());
    }
    code = StrUtil.removeSuffix(code, comma);
    StringBuilder builder = new StringBuilder("您好！您于");
    if (!CollectionUtils.isEmpty(artificialList)) {
      String artificialDate = UccpUtil.getArtificialSubmitDate(artificialList);
      if (!CollectionUtils.isEmpty(passbookList)) {
        builder.append(dateStr + "，提交单位报销的医药费票据共" + passbookList.size() + "张。");
      }
      builder.append(artificialDate + "，提交单位报销的医药费单据共" + artificialList.size() + "张，根据补助政策核定报销金额为" + sum + "元。报账单号：" + code);
    } else {
      builder.append(dateStr + "，提交单位报销的医药费票据共" + passbookList.size() + "张，根据补助政策核定报销金额为" + sum + "元。报账单号：" + code);
    }
    if (countTimeout > 0) {
      builder.append("有" + countTimeout + "张已超过报销时限不予报销。");
    }
    msg = builder.toString();
    TWiteoffInstance instance = instances.get(0);
    //您于XX月XX日-XX月XX日提交单位报销的医药费票据共XX张
    String phone = instance.getPhone();
//    phone = "18667018123";
    ShortMessageParam param = new ShortMessageParam(msg, phone);
    return vatService.sendShortMessage(param);
  }
  
}
